package cn.biplam.front.component.view.systemManage.sysprivi.user.manage.add;

import cn.biplam.common.bean.systemManage.privi.UserManageBean;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.UserOptCode;
import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.struc.SysStruc.UserStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.ComponentBase;
import cn.biplam.front.component.view.common.GenderSelect;
import cn.biplam.front.component.view.common.LanguageSelect;
import cn.biplam.front.component.view.systemManage.sysprivi.user.UserManage;
import cn.biplam.front.component.view.systemManage.sysprivi.user.manage.list.UserBase;
import cn.biplam.front.component.view.systemManage.sysprivi.user.manage.navi.UserNavi;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.ScriptTreeNew;
import cn.biplam.front.template.base.Input;
import cn.biplam.front.template.base.Input.Input_Type;
import cn.biplam.front.template.complex.InputPrompt;

/**
 *  修改用户信息
 * @author XiaoRuxing Dec 3, 200910:58:27 AM
 * @version 1.0
 */
public class UserBaseEdit extends ComponentBase{
	
	private static final String conId=UserManage.cId;

	public UserBaseEdit(UserInfo user, String homeUrl, String recordId,
			KvMap<String, String> params) {
		super(user, homeUrl, recordId, params);
	}

	/**
	 *  获取用户修改界面
	 * @param mUser    用户信息
	 * @param sHomeUrl 应用系统路径
	 * @param recordId 用户ID
	 * @return HtmlWindow 用户基本信息修改页面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow getEdit(String containerId) throws ErrorMessage{
		String cPageId = conId+".edit";
		//修改界面
		InputPrompt editInput = new InputPrompt(UserManage.User_Base_Info_Edit_Submit,
				recordId,
				cPageId);
		HtmlWindow edit = getUserInfoWin(mUser, sHomeUrl, recordId, cPageId, editInput);
		return edit;
	}
	
	/**
	 *  获取用户添加主界面
	 * @param mUser    用户信息
	 * @param sHomeUrl 应用系统路径
	 * @param recordId 用户所属组
	 * @return HtmlWindow 用户添加页面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow getAdd() throws ErrorMessage{
		String cPageId = conId+".add";
		recordId = params.getByKey(TreeParam.NODE_ID);
		//添加用户界面
		InputPrompt addInput = new InputPrompt(UserManage.User_Add_Submit,recordId,cPageId);
		HtmlWindow add = getUserInfoWin(mUser, sHomeUrl, recordId, cPageId, addInput);
		return add;
	}

	/**
	 *  修改用户信息入库
	 * @return 修改后刷新脚本
	 * @throws ErrorMessage
	 */
	public HtmlWindow editSubmit() throws ErrorMessage{
		//提交编辑用户信息入库
		DataFilter filter = getBaseFileter(params);
		filter.put(UserStruc.USERID, recordId);//用户ID
		Operator.get(UserOptCode.Edit_User, mUser.getUserID(), filter);
		//刷新用户树
		String script = AjxScript.getA(UserBase.conId, UserManage.User_Base_Info);
		HtmlWindow win = new HtmlDiv();
		win.bind(script);
		return win;
	}
	
	/**
	 *  修改用户信息入库
	 * @return 修改后刷新脚本
	 * @throws ErrorMessage
	 */
	public HtmlWindow addSubmit() throws ErrorMessage{
		//提交编辑用户信息入库
		UserManageBean userOpt = UserManageBean.getNewInstance(mUser.getUserID());
		OperatorResult rs = userOpt.insertUser(recordId, //用户组ID
				params.getByKey(UserStruc.USERNAME), //用户名称
				params.getByKey(UserStruc.PASSWORD), //用户密码
				params.getByKey(TOOLS.getSelectValue(params, UserStruc.LANG)),     //用户语言
				params.getByKey(UserStruc.USERCODE), //用户代码
				params.getByKey(TOOLS.getSelectValue(params, UserStruc.SEX)),      //用户性别
				params.getByKey(UserStruc.BIRTHEDAY), //生日
				params.getByKey(UserStruc.USER_MACHINE));
		String script = ScriptTreeNew.getRefreshAdd(
				params.getByKey(TreeParam.NODE_CONTARINER_ID),
				UserNavi.Expand,
				rs.getLastRowId());
		HtmlWindow win = new HtmlDiv();
		win.bind(script);
		return win;
	}
	
	/**
	 * @param params 信息容器
	 * @return 需要提交的用户基本信息
	 * @throws ErrorMessage 
	 */
	private DataFilter getBaseFileter(KvMap<String, String> params) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(UserStruc.SEX, TOOLS.getSelectValue(params, UserStruc.SEX));
		filter.put(UserStruc.LANG, TOOLS.getSelectValue(params, UserStruc.LANG));
		filter.putAll(params);
		return filter;
	}
	
	/**
	 * @return 用户基本信息界面
	 */
	private HtmlWindow getUserInfoWin(UserInfo mUser,String sHomeUrl,String recordId,String pageId,
			InputPrompt editComp) throws ErrorMessage{
		OperatorResult userInfo = getUserInfo(mUser.getUserID(), recordId);
		//用户基本信息
		String name = null;
		String sex = null;
		String password = null;
		String language = null;
		String userCode = null;
		String birthiday = null;
		String userMachine = null;
		if(TOOLS.exist(userInfo)){
			name = userInfo.getValue(UserStruc.USERNAME);
			sex = userInfo.getValue(UserStruc.SEX);
			password = userInfo.getValue(UserStruc.PASSWORD);
			language = userInfo.getValue(UserStruc.LANG);
			userCode = userInfo.getValue(UserStruc.USERCODE);
			birthiday = userInfo.getValue(UserStruc.BIRTHEDAY);
			userMachine = userInfo.getValue(UserStruc.USER_MACHINE);
		}
		editComp.putTh("姓名", 0, 0);
		editComp.putTd(new Input(null,UserStruc.USERNAME,name,20,50).get(), 0, 1,"3");
		
		editComp.putTh("密码",1,0);
		editComp.putTd(new Input(null,UserStruc.PASSWORD,Input_Type.password,password,20,20).get(),1,1,"3");
		
		editComp.putTh("性别", 2, 0);
		editComp.putTd(GenderSelect.get(UserStruc.SEX, sex, mUser, sHomeUrl), 2, 1);
		editComp.putTh("出生日期", 2, 2);
		HtmlWindow date = new Input(null,UserStruc.BIRTHEDAY,birthiday,10).get();
		editComp.putTd(date, 2, 3);
		
		editComp.putTh("编号", 3, 0);
		editComp.putTd(new Input(null,UserStruc.USERCODE,userCode,10,20).get(), 3, 1);
		editComp.putTh("语言种类", 3, 2);
		editComp.putTd(LanguageSelect.get(UserStruc.LANG, language, mUser, sHomeUrl), 3, 3);
		
		editComp.putTh("机器名称", 4, 0);
		editComp.putTd(new Input(null,UserStruc.USER_MACHINE,userMachine,20,50).get(), 4, 1,"3");
		editComp.setWidth("350");
		editComp.setColSize(3, 0, 8);
		editComp.setColSize(3, 2, 8);
		//设置输入项检查
		editComp.putCheckItem(UserStruc.USERNAME, "用户名称不能为空!");
		editComp.putCheckItem(UserStruc.PASSWORD, "密码不能为空!");
		//放入附加参数
		editComp.putParam(TreeParam.NODE_CONTARINER_ID, params.getByKey(TreeParam.NODE_CONTARINER_ID));
		return editComp.get();
	}

	/**
	 * @return 单个用户基本信息
	 */
	private OperatorResult getUserInfo(String userId, String recordId) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(UserStruc.USERID, recordId);
		OperatorResult rs = Operator.get(UserOptCode.Query_User_By_Id, userId, filter);
		return rs;
	}
}
